var item = {

    init:function(){
        //添加下拉框选择事件
        layui.form.on('submit(_save_)', function(data){
            return item.save(data);
        });

    },
    select:function(data){
        if($.trim(data.value) != ''){
            //获取小类信息
            njzxw.ajax({
                url:URL.ITEM.GETITEMLIST,
                data:{
                    group_id:data.value
                },
                fun:function(a,b){
                    if(a){
                        if(b.state == 1){
                            var is_add_item = b.is_add_item;//在多项中是否允许添加删除子类1可以0不能添加
                            var siglesubitem = b.siglesubitem;//是否是多项 1是0否
                            $('.smll_typ_div').empty();
                            if(siglesubitem != 1){//不是多项
                                item.disposs_dx(b.itemList,data.value);
                            }else{//是多项
                                if(is_add_item != 1){//不允许新增删除参数
                                    item.disposs_dx(b.itemList,data.value);
                                }else{
                                    item.disposs_duox(b.itemList,data.value);
                                }
                            }
                        }else{
                            layui.layer.alert(b.msg);
                        }
                    }else{
                        layui.layer.alert("请求错误，请重新操作");
                    }
                }
            })

        }else{

        }
    },
    addItem:function(obj){
        var html = '<tr>'
            + '  <td>'
            + '        <input type="text" value="" lay-verify="required" lay-verType="tips" name="itemName" id="itemName" style="" autocomplete="off" placeholder="请填写参数名称 " class="layui-input">'
            + '    </td>'
            + '    <td>'
            + '        <input type="text" value="" lay-verify="required" lay-verType="tips" name="itemValue" id="itemValue" style="" autocomplete="off" placeholder="请填写参数值" class="layui-input">'
            + '    </td>'
            + '    <td>'
            + '        <input type="text" value="" lay-verify="required" lay-verType="tips" name="itemOrder" id="itemOrder" style="" autocomplete="off" placeholder="请填写参数顺序" class="layui-input">'
            + '    </td>'
            + '    <td>'
            + '        <textarea type="text" value=""  name="itemRemark" id="itemRemark" style="" autocomplete="off" placeholder="请填写备注" class="layui-textarea"></textarea>'
            + '    </td>'
            + '    <td style="text-align: center;">'
            + '      <i class="layui-icon" onclick="item.delateItem(this);return false;" style="cursor: pointer;"></i>'
            + '    </td>'
            + ' </tr>';
        $('.smll_typ_div tbody').append(html);
        return false;
    },
    /**
     * 删除权限
     * @param obj
     */
    delateItem:function(obj){
        //清除
        $(obj).parent().parent().remove();
        return false;
    },
    disposs_duox:function(itemList,groupid){
        var html =  '<input type=\'hidden\' id=\'group_id\' name=\'group_id\' value='+groupid+' ><button onclick="item.addItem(this);return false;" class="layui-btn layui-btn-small addLine"><i class="layui-icon"></i> 添加</button><button id="saveBnt" class="layui-btn layui-btn-small addLine" lay-submit lay-filter="_save_" ><i class="fa  fa-floppy-o fa-fw"></i>&nbsp;&nbsp;保&nbsp;&nbsp;存&nbsp;&nbsp;</button>	<table class="layui-table">'+
            '  <thead>'+
            '    <tr>'+
            '      <th>参数名称</th>'+
            '      <th>参数值</th>'+
            '      <th>参数顺序</th>'+
            '      <th>参数备注</th>'+
            '      <th>操作</th>'+
            '    </tr> '+
            '  </thead>'+
            '  <tbody>';
        for(var i=0;i<itemList.length;i++){
            html += '<tr>'+
                '	<td>'+
                '      	<input type="hidden" value="'+itemList[i].id+'"  name="itemid" id="itemid" ><input type="text" value="'+itemList[i].name+'" datatype="Require" msg="请填写参数名称" name="itemName" id="itemName" style="" autocomplete="off" placeholder="请填写参数名称 " class="layui-input">'+
                '   </td>'+
                '   <td>'+
                '     	<input type="text" value="'+itemList[i].values+'" datatype="Require" msg="请填写参数值" name="itemValue" id="itemValue" style="" autocomplete="off" placeholder="请填写参数值" class="layui-input">'+
                '   </td>'+
                '   <td>'+
                '     	<input type="text" value="'+itemList[i].orderby+'" datatype="Number" msg="请填写参数顺序" name="itemOrder" id="itemOrder" style="" autocomplete="off" placeholder="请填写参数顺序" class="layui-input">'+
                '   </td>'+
                '    <td>'+
                '        <textarea type="text" value=""  name="itemRemark" id="itemRemark" style="" autocomplete="off" placeholder="请填写备注" class="layui-textarea">'+$.trim(itemList[i].remark)+'</textarea>'+
                '    </td>'+
                '   <td style="text-align: center;" >'+
                '   	<i class="layui-icon" onclick="item.delateItem(this);return false;" style="cursor: pointer;" ></i>'+
                '   </td>'+
                '</tr>';
        }
        html += '</tbody></table>';
        $('.smll_typ_div').html(html);
    },
    disposs_dx:function(itemList,groupid){
        if(itemList.length != 0){
            var html = "<div><input type='hidden' id='group_id' name='group_id' value='"+groupid+"' ><button id=\"saveBnt\" class=\"layui-btn layui-btn-normal\" lay-submit lay-filter=\"_save_\" ><i class=\"fa  fa-floppy-o fa-fw\"></i>&nbsp;&nbsp;保&nbsp;&nbsp;存&nbsp;&nbsp;</button>	</div>";
            for(var i=0;i<itemList.length;i++){
                html += "<div class=\"layui-form-item\" style=\"vertical-align: text-top;\" >"+
                    "<label class=\"layui-form-label\">"+itemList[i].name+"参数值</label>"+
                    "<div class=\"layui-input-block \"><input type='hidden' id='item_id' name='item_id' value='"+itemList[i].id+"' >"+
                    "	<input type=\"text\" value='"+itemList[i].values+"' name=\"values\" id=\"values\" datatype=\"Require\" msg=\"请填写"+itemList[i].name+"参数值\" autocomplete=\"off\" placeholder=\"请填写"+itemList[i].name+"参数值\" class=\"layui-input\">"+
                    "</div>"+
                    "<div style=\"max-width: 300px;\" >"+
                    "	参数说明："+ ((itemList[i].explain == undefined || itemList[i].explain == '' || itemList[i].explain == null || itemList[i].explain == 'null')?"无":itemList[i].explain)+
                    "</div>"+
                    "</div>";
            }
            $('.smll_typ_div').append(html);
        }else{
            var html = "<div><input type='hidden' id='group_id' name='group_id' value='"+groupid+"' ><button id=\"saveBnt\" class=\"layui-btn layui-btn-normal\" lay-submit lay-filter=\"_save_\" ><i class=\"fa  fa-floppy-o fa-fw\"></i>&nbsp;&nbsp;保&nbsp;&nbsp;存&nbsp;&nbsp;</button>	</div>";
            html += "<div class=\"layui-form-item\" style=\"vertical-align: text-top;\" >"+
                "<label class=\"layui-form-label\">参数值</label>"+
                "<div class=\"layui-input-block \">"+
                "	<input type=\"text\" value='' name=\"values\" id=\"values\" datatype=\"Require\" msg=\"请填写参数值\" autocomplete=\"off\" placeholder=\"请填写参数值\" class=\"layui-input\">"+
                "</div>"+
                "</div>";
            $('.smll_typ_div').append(html);
        }
    },
    save:function(data){
        var data = njzxw.formData('#itemform');
        if(typeof(data.itemOrder) == 'string'){
            data.itemName = [data.itemName];
            data.itemOrder = [data.itemOrder];
            data.itemRemark = [data.itemRemark];
            data.itemValue = [data.itemValue];
            data.itemid = [data.itemid];
        }
        console.log(data);
        njzxw.ajax({
            url:URL.ITEM.SAVEITEM,
            data:data
        })

        return false;
    },
}